/*
 * @Description: enter your description
 * @Autor: 帅ZR帅
 * @Date: 2023-10-08 20:42:06
 * @LastEditors: 帅ZR帅
 * @LastEditTime: 2023-10-26 18:31:46
 */
#include "RFID.h"
#include <stdio.h>
#include <stdlib.h>
#include <HardwareSerial.h>
#include "queue.h"
#include <string.h>
#include "math.h"
#include "algorithm"
#include "cstdio"
#include "Bluetooth.h"
#include "OLED.h"
#include <U8g2lib.h>
#include "SSEncryptor.h"
HardwareSerial UART_RFID(0);
QueueHandle_t RFID_QUEUE;
SemaphoreHandle_t RFID_RECEIVE;

//**************************************RFID模块**************************************************//
int MCU_XC9142 = 38;  // RFID 电源使能 高电平有效(输出)
int MCU_RFID_EN = 40; // RFID使能//
int U0TXD = 37;       // ESP串口0TX
int U0RXD = 36;       // ESP串口0RX
//**************************************RFID模块**************************************************//
char UART_DATA_RFID_PER;     // 串口每次接收到的数据
uint8_t UART_DATA_RFID[100]; // 串口接收数组
// int len = 0;
// int invalid_data_num = 0; // 无效数据次数
uint8_t UART_DATA_RFID_NUM = 0;
uint16_t UART_DATA_RFID_DATA[10];
uint8_t UART_DATA_RFID_DATA_NUM = 0;

uint8_t RFID_check[8] = {0XBB, 0X00, 0X03, 0X00, 0X01, 0X00, 0X04, 0X7E};
uint8_t RFID_scan[7] = {0XBB, 0X00, 0X22, 0X00, 0X00, 0X22, 0X7E};
uint8_t power_check[7] = {0XBB, 0X00, 0XB7, 0X00, 0X00, 0XB7, 0X7E};
uint8_t power_set_15db[9] = {0XBB, 0X00, 0XB6, 0X00, 0X02, 0X05, 0XDC, 0X99, 0X7E}; // 20db
uint8_t power_set_20db[9] = {0XBB, 0X00, 0XB6, 0X00, 0X02, 0X07, 0XD0, 0X8F, 0X7E}; // 20db
uint8_t power_set_26db[9] = {0XBB, 0X00, 0XB6, 0X00, 0X02, 0X0A, 0X28, 0XEA, 0X7E}; // 26db

uint8_t set_CH_900[8] = {0XBB, 0X00, 0X7, 0X00, 0X01, 0X01, 0X09, 0X7E};
uint8_t set_920_125HZ[8] = {0XBB, 0X00, 0XAB, 0X00, 0X01, 0X00, 0XAC, 0X7E}; // 1.1m
uint8_t set_921_125HZ[8] = {0XBB, 0X00, 0XAB, 0X00, 0X01, 0X04, 0XB0, 0X7E}; // 1.1m
uint8_t set_922_125HZ[8] = {0XBB, 0X00, 0XAB, 0X00, 0X01, 0X08, 0XB4, 0X7E}; // 0.9m
uint8_t set_923_125HZ[8] = {0XBB, 0X00, 0XAB, 0X00, 0X01, 0X0C, 0XB8, 0X7E}; // 0.8m
uint8_t set_924_125HZ[8] = {0XBB, 0X00, 0XAB, 0X00, 0X01, 0X0F, 0XBB, 0X7E}; // 0.8m

uint8_t set_US[8] = {0XBB, 0X00, 0X7, 0X00, 0X01, 0X02, 0X0A, 0X7E};
uint8_t set_902_25HZ[8] = {0XBB, 0X00, 0XAB, 0X00, 0X01, 0X00, 0XAC, 0X7E}; // 1.1m

uint8_t set_auto_fre[8] = {0XBB, 0X00, 0XAD, 0X00, 0X01, 0XFF, 0XAD, 0X7E};

// CH 900 BB 00 07 00 01 01 09 7E
// 924.125 BB 00 AB 00 01 0F BB 7E
// 921.125 BB 00 AB 00 01 04 B0 7E
// 920.125 BB 00 AB 00 01 00 AC 7E

// US  BB 00 07 00 01 02 0A 7E
// 902.25 BB 00 AB 00 01 00 AC 7E

// GET FRE BB 00 AA 00 00 AA 7E

// SCAN BB 00 22 00 00 22 7E
// 26DB BB 00 B6 00 02 0A 28 EA 7E
// AUTO FRE FLASE : BB 00 AD 00 01 00 AE 7E / true BB 00 AD 00 01 FF AD 7E
void RFID_COMMAND(uint8_t *command)
{
    uint8_t command_i = 0;
    do
    {
        UART_RFID.write(command[command_i]);
    } while (command[command_i++] != 0X7E);
}
void RFID_Data_Analysis(void)
{
    int i = 0;
    do
    {
        if (pdPASS == xQueueReceive(RFID_QUEUE, (void *)&UART_DATA_RFID_PER, 0))
        {
            if (UART_DATA_RFID_PER == 0XBB)
            {
                memset(UART_DATA_RFID, 0, sizeof(UART_DATA_RFID));
                i = 0;
                UART_DATA_RFID[i] = UART_DATA_RFID_PER;
            }
            else
            {
                UART_DATA_RFID[++i] = UART_DATA_RFID_PER;
            }
            // char *newValue = UART_DATA_RFID;
            // pCharacteristic->setValue(newValue);
            // pCharacteristic->notify();
        }
    } while (UART_DATA_RFID_PER != 0X7E); // #####!!!!!!!!!!!!!!!!
    if (UART_DATA_RFID_PER == 0X7E && i > 1)
    {
        uint16_t sum = 0;
        for (int j = 1; j <= i - 2; j++)
        {
            sum += UART_DATA_RFID[j];
        }
        sum = sum & 0xFF;
        if (sum == UART_DATA_RFID[i - 1])
        {
            if (UART_DATA_RFID[1] == 0x02 && UART_DATA_RFID[2] == 0x22)
            {
                memset(list, 0, sizeof(list));
                int z = 0;
                for (int j = 8; j <= i - 4; j++)
                {
                    list[z++] = UART_DATA_RFID[j] / 16 + 48;
                    list[z++] = UART_DATA_RFID[j] % 16 + 48;
                }
                // char *newValue = list;
                // pCharacteristic->setValue(newValue);
                // pCharacteristic->notify();
                RFID_Flag = 0;
                Decryptor();
            }
        }
    }
}

void RFID_READ(void)
{
    if (UART_RFID.available())
    {
        char reveive_data[1];
        reveive_data[0] = UART_RFID.read();
        xQueueSend(RFID_QUEUE, (void *)&reveive_data, 0);
        if (reveive_data[0] == 0X7E)
        {
            xSemaphoreGive(RFID_RECEIVE);
        }
    }
    else if (pdTRUE == xSemaphoreTake(RFID_RECEIVE, 50)) //
    {
        RFID_Data_Analysis();
    }
}
void RFID_Init(void)
{
    pinMode(MCU_RFID_EN, OUTPUT); // OLED 电源引脚初始化
    digitalWrite(MCU_RFID_EN, HIGH);

    pinMode(MCU_XC9142, OUTPUT); // OLED 电源引脚初始化
    digitalWrite(MCU_XC9142, LOW);

    RFID_QUEUE = xQueueCreate(100, sizeof(char));
    RFID_RECEIVE = xSemaphoreCreateBinary();
    UART_RFID.begin(115200);
    vTaskDelay(500 / portTICK_PERIOD_MS);

    RFID_COMMAND(set_CH_900);
    RFID_COMMAND(set_920_125HZ); // 1.2m

    // RFID_COMMAND(set_CH_900);
    // RFID_COMMAND(set_auto_fre);

    // RFID_COMMAND(set_US);
    // RFID_COMMAND(set_902_25HZ);

    // RFID_COMMAND(power_set_26db);

    uint8_t power_high = (rfid_list[0].data * 100) >> 8;
    uint8_t power_low = (rfid_list[0].data * 100) & 0xff;
    uint8_t Ver = (0XB6 + 0X02 + power_high + power_low) & 0xff;
    uint8_t power_set_db[9] = {0XBB, 0X00, 0XB6, 0X00, 0X02, power_high, power_low, Ver, 0X7E};
    RFID_COMMAND(power_set_db);
}